package com.fanbuchi.app.ins.adapter;


import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fanbuchi.app.ins.R;
import com.fanbuchi.app.ins.event.OnClickEvent;
import com.fanbuchi.app.ins.image.ImageManager;
import com.fanbuchi.app.ins.ui.LoginActivity;
import com.fanbuchi.app.ins.vo.HotPoint;
import com.fanbuchi.app.live.PLVideoViewActivity;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by rabbit on 2016/3/16.
 */
public class RecyclerAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements  View.OnClickListener,View.OnLongClickListener{

    List<HotPoint> mPictures;
    public RecyclerAdapter(List<HotPoint> pictures){
        this.mPictures=pictures;
        if (this.mPictures==null){
            new IllegalArgumentException("not allows null paramater");
        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View viewSimple = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.content_simple_item, parent, false);
        RecyclerView.ViewHolder vh = new ViewHolderSimple(viewSimple);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        onBindViewHolderSimple((ViewHolderSimple) holder, position);
    }

    public void onBindViewHolderSimple(final ViewHolderSimple holder, int position) {
       final HotPoint  picture=mPictures.get(position);
        Uri uri = Uri.parse(picture.links);
        holder.itemView.setOnClickListener(this);
        holder.mSimpleDraweeView.setImageURI(uri);
        ImageManager.showThumb(uri, holder.header);
        holder.header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(),LoginActivity.class);
                holder.itemView.getContext().startActivity(intent);
            }
        });
    }





    @Override
    public int getItemCount() {
        return mPictures.size();
    }

    @Override
    public void onClick(View v) {
        EventBus.getDefault().post(new OnClickEvent());
    }

    @Override
    public boolean onLongClick(View v) {
        return false;
    }


    public static class ViewHolderSimple extends RecyclerView.ViewHolder {
        SimpleDraweeView mSimpleDraweeView;
        SimpleDraweeView header;
        public ViewHolderSimple(View itemView) {
            super(itemView);
            mSimpleDraweeView=(SimpleDraweeView)itemView.findViewById(R.id.big);
            header=(SimpleDraweeView)itemView.findViewById(R.id.header);
        }
    }


}
